package com.openbravo.pos.panels.navigator;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.tree.TreeCellRenderer;

import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.dao.user.tree.BrowsableEditableTreeDataModel;

import com.openbravo.bean.Location;
import com.openbravo.bean.LocationLayout;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.JDragAbleTreeNavigator;
import com.openbravo.data.gui.TreeCellRendererBasic;
import com.openbravo.data.loader.IRenderString;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.data.user.EditorRecordAdapter;
import com.openbravo.pos.panels.navigator.event.LocationLayoutChangedListener;

public class JLocationLayoutNavigator extends JDragAbleTreeNavigator<LocationLayout> {

	private I_DataLogic dataLogic;

	public JLocationLayoutNavigator(I_DataLogic dataLogic) {
		super(new BrowsableEditableTreeDataModel(dataLogic,LocationLayout.class,null,new EditorRecordAdapter<LocationLayout>(),new DirtyManager()));
	    setCellRenderer(getTreeCellRenderer());
	    this.dataLogic=dataLogic;
	}

	private TreeCellRenderer getTreeCellRenderer() {
		return new TreeCellRendererBasic(new IRenderString<LocationLayout>(){
            public String getRenderString(LocationLayout value) {
               return value.getName();
            }
        });
	}
	
	@Override
	public void valueChanged(TreeSelectionEvent evt) {
		super.valueChanged(evt);
		for(LocationLayoutChangedListener l:listenerList.getListeners(LocationLayoutChangedListener.class))
			l.valueSelectChanged( (LocationLayout) m_bd.getSelectedObj().getCategory());
	}

	public void addLocationLayoutChangedListener(LocationLayoutChangedListener l){
		listenerList.add(LocationLayoutChangedListener.class, l);
	}
	
	public void removeLocationLayoutChangedListener(LocationLayoutChangedListener l){
		listenerList.remove(LocationLayoutChangedListener.class, l);
	}


	public void loadData(String locationId) throws BasicException{
			m_bd.loadData(dataLogic.list(LocationLayout.class,Location.class,locationId));
   }
}
